package ctn.batch.thread;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ctn.contant.MsgContant;
import ctn.dao.inteface.MessageDao;
import ctn.mail.EmailSend;
public class SendEmailThread extends Thread{

	private EmailSend es;
	private List<Map<String, String>> list;
	private MessageDao messageDaoImpl;
	private Log log = LogFactory.getLog(SendEmailThread.class);

	public void setList(List<Map<String, String>> list) {
		this.list = list;
	}

	public SendEmailThread(){
		
	}
	
	public SendEmailThread(MessageDao messageDaoImpl,EmailSend es,List<Map<String, String>> list){
		this.es = es;
		this.messageDaoImpl = messageDaoImpl;
		this.list = list;
	}
	
	public void setEs(EmailSend es) {
		this.es = es;
	}


	@Override
	public void run() {
		log.info("Sending the Email thread run..................");
		for (Map<String, String> map : list) {
			log.info("To mail:"+map.get(MsgContant.MESSAGE_TO_MAIL));
			es.setTomail(map.get(MsgContant.MESSAGE_TO_MAIL));
			log.info("Email subject:"+map.get(MsgContant.MESSAGE_SUBJECT));
			es.setSubject(map.get(MsgContant.MESSAGE_SUBJECT));
			log.info("Email body:"+map.get(MsgContant.MESSAGE_CONTENT));
			es.setContent(map.get(MsgContant.MESSAGE_CONTENT));
			log.info("Sending the Email...................");
			boolean flag = es.sendMail();
			if (!flag) {
				log.info("Sending the Email...................Failure");
				messageDaoImpl.updateMsgStatus(MsgContant.MESSAGE_STATUS_FAILTURE, Integer.parseInt(map.get(MsgContant.MESSAGE_ID)));
			}else{
				log.info("Sending the Email...................Success");
				messageDaoImpl.updateMsgStatus(MsgContant.MESSAGE_STATUS_SUCCESS, Integer.parseInt(map.get(MsgContant.MESSAGE_ID)));
			}
		}
		log.info("Sending the Email...................Complete");
	}
	
}
